﻿#region

using System;
using System.IO;
using System.Text;
using log4net;

#endregion

namespace Jiubang.Wap.Filter{
    ///<summary>
    /// 数据流过滤抽象类。<br/>
    /// 重写数据流的Write方法，添加以下功能：
    /// 1、将指定的数据流转换成文本。(UTF8格式)<br/>
    /// 2、调用过滤方法对文本进行过滤。<br/>
    /// 3、将过滤后的文本以UTF8格式转换为byte数据后写入数据流之中。
    ///</summary>
    public abstract class AbstractFilter : Stream{
        private readonly ILog log = LogManager.GetLogger(typeof (AbstractFilter));
        private readonly Stream stream;

        ///<summary>
        ///</summary>
        ///<param name = "stream"></param>
        protected AbstractFilter(Stream stream){
            this.stream = stream;
        }

        ///<summary>
        ///</summary>
        public override bool CanRead{
            get { return stream.CanRead; }
        }

        ///<summary>
        ///</summary>
        public override bool CanSeek{
            get { return stream.CanSeek; }
        }

        ///<summary>
        ///</summary>
        public override bool CanWrite{
            get { return stream.CanWrite; }
        }

        ///<summary>
        ///</summary>
        public override long Length{
            get { return stream.Length; }
        }

        ///<summary>
        ///</summary>
        public override long Position{
            get { return stream.Position; }
            set { stream.Position = value; }
        }

        ///<summary>
        ///</summary>
        public override void Flush(){
            stream.Flush();
        }

        ///<summary>
        ///</summary>
        ///<param name = "offset"></param>
        ///<param name = "origin"></param>
        ///<returns></returns>
        public override long Seek(long offset, SeekOrigin origin){
            return stream.Seek(offset, origin);
        }

        ///<summary>
        ///</summary>
        ///<param name = "value"></param>
        public override void SetLength(long value){
            stream.SetLength(value);
        }

        ///<summary>
        ///</summary>
        ///<param name = "buffer"></param>
        ///<param name = "offset"></param>
        ///<param name = "count"></param>
        ///<returns></returns>
        public override int Read(byte[] buffer, int offset, int count){
            return stream.Read(buffer, offset, count);
        }

        ///<summary>
        ///</summary>
        ///<param name = "buffer"></param>
        ///<param name = "offset"></param>
        ///<param name = "count"></param>
        public override void Write(byte[] buffer, int offset, int count){
            var rawText = Encoding.UTF8.GetString(buffer, offset, count);
            string replacedText;
            try{
                replacedText = TextFilter(rawText);
            } catch (Exception ex){
                log.Error(ex);
                replacedText = rawText;
            }
            var bytes = Encoding.UTF8.GetBytes(replacedText);
            stream.Write(bytes, 0, bytes.Length);
        }

        ///<summary>
        ///  对指定内容进行过滤的方法
        ///</summary>
        ///<param name = "text"></param>
        ///<returns></returns>
        public abstract string TextFilter(string text);
    }
}